package operation;

import book.BookList;

import java.util.Scanner;

public class SearchOperation implements IOperation{

    public int search(BookList bookList,String bookName) {
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if (bookName.equals(bookList.getBook(i).getName())) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void work(BookList bookList) {
        System.out.println("请输入需要查询的书名: ");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();
        int index = search(bookList,bookName);
        if (index != -1) {
            System.out.println(bookList.getBook(index));
        } else {
            System.out.println("未查询到名字为《"+bookName+"》的图书!");
        }
    }
}
